import { message } from 'antd';
import config from '../../comps/business/tradingMarket/config';
import api from '../../api';
export default {
  namespace: 'stockGroupOrderM',
  state: {
    clumns: [
      {
        title: '证券代码',
        dataIndex: 'securityCode',
        key: 'securityCode',
        sorter: (a, b) => a.securityCode - b.securityCode,
      },
      {
        title: '证券名称',
        dataIndex: 'securityName',
        key: 'securityName',
        width: '140px',
        render: (val, record) => {
          if (val) {
            if (record.securityType == 0) {
              return (
                <div>
                  {val}
                  <span style={{ display: 'inline-block', width: '36px', textAlign: 'center', borderRadius: '3px', backgroundColor: '#F0F3F8', color: '##000000', float: 'right' }}>
                    停牌
                  </span>
                </div>
              );
            } else if (record.securityType == 1) {
              return (
                <div>
                  {val}
                  <span
                    style={{
                      display: 'inline-block',
                      width: '36px',
                      textAlign: 'center',
                      borderRadius: '3px',
                      backgroundColor: 'rgba(244, 66, 58, 0.1);',
                      color: '#F4423A',
                      float: 'right',
                    }}
                  >
                    涨停
                  </span>
                </div>
              );
            } else if (record.securityType == 2) {
              return (
                <div>
                  {val}
                  <span
                    style={{
                      display: 'inline-block',
                      width: '36px',
                      textAlign: 'center',
                      borderRadius: '3px',
                      backgroundColor: 'rgba(47, 176, 152,0.1)',
                      color: '#2FB098',
                      float: 'right',
                    }}
                  >
                    跌停
                  </span>
                </div>
              );
            } else {
              return val;
            }
          } else {
            return '--';
          }
        },
      },
      {
        title: '交易市场',
        dataIndex: 'exchangeCode',
        key: 'exchangeCode',
        width: '110px',
        render: (val, record) => {
          if (typeof val == 'object') {
            return val;
          }
          let findvalue = config.filter((item) => {
            return item.value == val;
          });
          if (findvalue && findvalue.length > 0) {
            return findvalue[0].title;
          } else {
            return '--';
          }
        },
      },
      {
        title: '价格(元)',
        dataIndex: 'price',
        key: 'price',
        type: 'money',
        width: '110px',
      },
      {
        title: '涨跌幅',
        dataIndex: 'priceLimit',
        key: 'priceLimit',
        align: 'right',
        width: '110px',
        render: (text) => {
          if (text === null || text === undefined) {
            return '--';
          } else {
            // NaN: Not a Number
            // isNaN()的缺点就在于 null、空格以及空串会被按照0来处理
            if (isNaN(text)) {
              return text;
            } else {
              return <span style={{ color: Number(text) == 0 ? '#000' : Number(text) > 0 ? '#FF0100' : '#00A62F' }}>{(Number(text) * 100).toFixed(2) + '%'}</span>;
            }
          }
        },
      },
      {
        title: '权重分配',
        dataIndex: 'allocateWeight',
        key: 'allocateWeight',
        align: 'right',
        width: '136px',
        sorter: (a, b) => a.allocateWeight - b.allocateWeight,
      },
      {
        title: '理论分配金额(元)',
        dataIndex: 'theoreticalAmount',
        key: 'theoreticalAmount',
        type: 'money',
        width: '170px',
        sorter: (a, b) => a.theoreticalAmount - b.theoreticalAmount,
      },
      {
        title: '理论分配股票数',
        dataIndex: 'theoreticalNumber',
        key: 'theoreticalNumber',
        width: '170px',
        type: 'number',
      },
      {
        title: '实际分配数量',
        dataIndex: 'realityNumber',
        key: 'realityNumber',
        width: '170px',
        type: 'number',
      },
      {
        title: '实际分配金额(元)',
        dataIndex: 'realityMoney',
        key: 'realityMoney',
        type: 'money',
        width: '170px',
        sorter: (a, b) => a.realityMoney - b.realityMoney,
      },
      {
        title: '偏离度',
        dataIndex: 'degreeOfDeviation',
        key: 'degreeOfDeviation',
        align: 'right',
        width: '110px',
        render: (text) => {
          if (text === null || text === undefined) {
            return '--';
          } else {
            // NaN: Not a Number
            // isNaN()的缺点就在于 null、空格以及空串会被按照0来处理
            if (isNaN(text)) {
              return text;
            } else {
              return <span style={{ color: Number(text) == 0 ? '#000' : Number(text) > 0 ? 'red' : 'green' }}>{(Number(text) * 100).toFixed(2) + '%'}</span>;
            }
          }
        },
      },
      {
        title: '行业',
        dataIndex: 'industry',
        key: 'industry',
        width: '110px',
        sorter: (a, b) => a.industry.localeCompare(b.industry, 'zh'),
      },
      {
        title: '操作',
        dataIndex: 'operate',
        key: 'operate',
        type: 'operate',
        width: 120,
        fixed: 'right',
      },
    ],
  },

  effects: {
    // 表格数据
    *getTableData({ payload, callback }, { call, put }) {
      const response = yield call(api.stockGroupOrder.getTableData, payload);

      if (response && response.code == 0) {
        callback(response);
      } else {
        callback();
        message.error(response?.msg || '计算失败!');
      }
    },

    // 导入文件
    *upLoad({ payload, callback }, { call, put }) {
      const response = yield call(api.stockGroupOrder.upLoad, payload);
      if (response && response.code == 0) {
        callback(response.importVo);
      } else {
        callback();
        message.error(response?.msg);
      }
    },
  },
  reducers: {
    save(state, { payload }) {
      return { ...state, ...payload };
    },
  },
};
